package com.wilmer.ai.controller;

import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.OpenAiStreamClient;
import com.unfbx.chatgpt.entity.chat.*;
import com.unfbx.chatgpt.sse.ConsoleEventSourceListener;
import com.wilmer.ai.client.StreamClient;
import com.wilmer.ai.dto.userChatDto;
import com.wilmer.ai.listener.SseStreamFastGptListener;
import com.wilmer.ai.request.ChatRequest;
import com.wilmer.ai.response.ChatResponse;
import com.wilmer.ai.service.SseService;
import com.wilmer.ai.service.impl.SseServiceImpl;
import com.wilmer.common.annotation.Anonymous;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.common.utils.StringUtils;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/ai")
public class tcontroller {

    @Resource
    private StreamClient client;
    @Resource
    private SseService service;

    @Autowired
    SseServiceImpl setMsg;

    @PostMapping("/kkk")
    @Anonymous
    /**
     * 拼接方式 URL?msg=xxx&uid=xxx  uid有则传，无则传空
     */
    public SseEmitter chat(@RequestParam String msg, @RequestParam String uid, HttpServletRequest request){
        ChatRequest chatRequest= new ChatRequest();

        if(StringUtils.isEmpty(uid)){
            long randomNumber = ThreadLocalRandom.current().nextLong(1000000, 10000000);
            uid = String.valueOf(randomNumber);
        }

        chatRequest.setMsg(msg);
        SseEmitter sse = service.createSse(uid);
        ChatResponse chatResponse = service.sseChat(uid,chatRequest,request);
        return sse;
    }
    @PostMapping("/chat")
    public SseEmitter chatWithAi(@RequestBody userChatDto chatDto){
        return service.chatWithAi(chatDto);
    }


    @PostMapping( "/sss")
    public SseEmitter chat2(){
        System.out.println(SecurityUtils.getUserId()+"---------------");
        String msg="你好";
        ChatRequest chatRequest= new ChatRequest();
        chatRequest.setMsg(msg);
        SseEmitter sse = service.createSse(msg + 10086);
        ChatResponse chatResponse = service.sseChatWithAi(msg+10086, chatRequest);
        return sse;
    }


//    @Anonymous
//    @GetMapping("/kkkk")
//    public ChatResponse yy(@RequestParam String msg) {
//        ChatRequest chatRequest= new ChatRequest();
//        chatRequest.setMsg(msg);
//        service.createSse("msg"+10086);

//        ChatResponse chatResponse = service.sseChat("msg"+10086, chatRequest);


        //聊天
//
////        Message message = Message.builder().role(Message.Role.USER).content(msg).build();
////
////        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
////        SseEmitter sseEmitter = new SseEmitter(-1L);
////        SseStreamFastGptListener listener = new SseStreamFastGptListener(sseEmitter);
////        OpenAiStreamClient openAiStreamClient = client.openAiStreamClient();
////        openAiStreamClient.streamChatCompletion(chatCompletion, listener);
//        //
//        Content textContent = Content.builder().text("根据图片里的内容写一个故事").type(Content.Type.TEXT.getName()).build();
//        ImageUrl imageUrl = ImageUrl.builder().url("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg").build();
//        Content imageContent = Content.builder().imageUrl(imageUrl).type(Content.Type.IMAGE_URL.getName()).build();
//        List<Content> contentList = new ArrayList<>();
//        contentList.add(textContent);
//        contentList.add(imageContent);
//        MessagePicture message = MessagePicture.builder().role(Message.Role.USER).content(contentList).build();
//        ChatCompletionWithPicture chatCompletion = ChatCompletionWithPicture
//                .builder()
//                .messages(Collections.singletonList(message))
//                .model(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName())
//                .build();
//        SseEmitter sseEmitter = new SseEmitter(-1L);
//        SseStreamFastGptListener listener = new SseStreamFastGptListener(sseEmitter);
//        OpenAiStreamClient openAiStreamClient = client.openAiStreamClient();
//        openAiStreamClient.streamChatCompletion(chatCompletion, listener);
////        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
////        chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
//
//
//
//
//
//
////        CountDownLatch countDownLatch = new CountDownLatch(1);
////        countDownLatch.countDown();
////        System.out.println(eventSourceListener);
//
////        try {
////            countDownLatch.countDown();
////        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        return chatResponse;
//    }



}
